﻿Imports System.Data.SqlClient
Imports System.IO

Public Class frmConfirmOrder

    Dim FolderPath As String = FTPAddress + "Prospects Cases"

    Private Sub frmConfirmOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.LabelResult.Text = "Count: " & Me.GridProspects.Rows.Count
        Me.WindowState = FormWindowState.Maximized
        FillProspect()
    End Sub

    Sub FillProspect()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select SNo,PNo,ProsNo,ProsType,Status,ProspectName,InsuranceCompany,InsuranceBroker,ClassofBusiness,BusinessSize" & _
                                      ",ExpiryDate,IsNull(AssignedTo,N'') AssignedTo," & _
                                      "IsNull(AssignedBy,N'') AssignedBy,IsNull(AssignedDate,N'') AssignedDate,SavedUser," & _
                                      "TransDate,dbo.GetProspectLastUpdate(SNo) LastUpdate,SentToUR From " & _
                                      "ProspectCases Where Branch ='" & Branch & "' and Status=N'Received'", cnn)
            Dim Reader As SqlDataReader
            Dim AssignDate As String

            Me.GridProspects.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                If Reader.Item("AssignedDate").ToString = "1/1/1900 12:00:00 AM" Then
                    AssignDate = ""
                Else
                    AssignDate = CDate(Reader.Item("AssignedDate")).ToString("yyyy/MM/dd")
                End If

                Me.GridProspects.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("ProsNo"), Reader.Item("Status"),
                                                        Format(CDate(Reader.Item("ExpiryDate")), "yyyy/MM/dd"), Reader.Item("ProspectName"), _
                                                        Reader.Item("InsuranceCompany"), Reader.Item("InsuranceBroker"), _
                                                        Reader.Item("ClassofBusiness"), Reader.Item("BusinessSize"), _
                                                        Reader.Item("AssignedTo"), Reader.Item("AssignedBy"), AssignDate, _
                                                        Reader.Item("SavedUser"), Format(CDate(Reader.Item("TransDate")), "yyyy/MM/dd"), _
                                                        CDate(Reader.Item("LastUpdate")).ToString("yyyy/MM/dd")})

            End While
            cnn.Close()

            'Check Colors
            For Each row As DataGridViewRow In Me.GridProspects.Rows
                If row.Cells(Me.DGColStatus.Index).Value = "Received" Then
                    row.Cells(Me.DGColStatus.Index).Style.BackColor = Color.Khaki
                End If
            Next


            'Check Expiry Date
            For Each Row As DataGridViewRow In Me.GridProspects.Rows
                If Now.Date >= CType(Row.Cells(3).Value, Date).Date Then
                    Row.Cells(3).Style.BackColor = Color.Red
                Else
                    Row.Cells(3).Style.BackColor = Color.White
                End If
            Next

            Me.LabelResult.Text = "Count: " & Me.GridProspects.Rows.Count

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            'MsgBox(ex.ToString)
        End Try

    End Sub


    Sub FillProspectsDocuments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Dir As New IO.DirectoryInfo(FolderPath & "\" & Me.GridProspects.CurrentRow.Cells(1).Value) 'sub is prospect no!
            Dim DirFiles As IO.FileInfo() = Dir.GetFiles()

            Me.GridFiles.Rows.Clear()

            'list the names of all files in the specified directory
            For Each Fi As FileInfo In DirFiles
                Me.GridFiles.Rows.Add(New String() {Fi.Name, Fi.CreationTime.ToString("yyyy/MM/dd hh:mm tt"), "Open", "Delete"})
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Try
            Me.OpenFileDialog1.Filter = "All files (*.*)|*.*"
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim File As New FileInfo(Me.OpenFileDialog1.FileName)
                File.CopyTo(FolderPath & "\" & Me.GridProspects.CurrentRow.Cells(1).Value & "\" & File.Name)

                FillProspectsDocuments()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub GridFiles_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridFiles.CellClick
        If e.ColumnIndex = 2 Then
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim FilePath As String = FolderPath + "\" + Me.GridProspects.CurrentRow.Cells(1).Value + "\" + Me.GridFiles.Rows(e.RowIndex).Cells(0).Value
                System.Diagnostics.Process.Start(FilePath)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub GridProspects_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridProspects.CurrentCellChanged
        Try
            Me.Cursor = Cursors.WaitCursor

            If Me.GridProspects.CurrentRow Is Nothing Then
                Me.Cursor = Cursors.Default
            Else
                FillProspectsDocuments()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub GridProspects_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridProspects.CellDoubleClick
        Dim a As New frmProspectsCasesStatus
        a.SNo = CInt(Me.GridProspects.CurrentRow.Cells(0).Value)
        a.ShowDialog()
        FillProspect()
    End Sub
End Class